package groups;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import lecture.Lecture;
import lecture.LectureManagedBean;
import user.User;

/**
 *
 * @author Thorsten Ludwig
 */
public class UserAssigningManagedBean {
    
    private User user;
    
    private boolean takePart;
    
    private ExerciseGroup eg;
    
    public UserAssigningManagedBean(User user, ExerciseGroup eg, boolean takePart) {
        if (user != null)
            System.out.println("Erstelle neuen User Assigning Bean für " + user.getUsername());
        else
            System.out.println("Erstelle neuen blabla mit " + eg + " " + takePart);
        this.user = user;
        this.eg = eg;
        this.takePart = takePart;
    }

    public ExerciseGroup getEg() {
        return eg;
    }
    
    public void groupChanged(ValueChangeEvent v) {
        System.out.println("Gruppenwechsel aufgerufen");
        ExerciseGroup oldEg = (ExerciseGroup) v.getOldValue();
        ExerciseGroup newEg = (ExerciseGroup) v.getNewValue();

        
        if (oldEg != null) {
            System.out.println("Entferne " + this.user.getUsername() + " aus " + oldEg.getName());
            oldEg.removeStudent(user.getUsername());
        }
        
        if (newEg != null) {
            System.out.println("Adde " + this.user.getUsername() + " zu " + newEg.getName());
            newEg.addStudent(user.getUsername());
        }  
    }

    public void setEg(ExerciseGroup eg) {
        this.eg = eg;
    }

    public User getUser() {
        return user;
    }

    public boolean isTakePart() {
        return takePart;
    }

    public String toggle() {
        System.out.println("Toggle Wert von " + this.user.getUsername());
        this.takePart = !this.takePart;
        return "exerciseGroupAssigning.jsf";
    }
    
    public void setTakePart(boolean takePart) {
        System.out.println("Set Take Part von " + this.user.getUsername() + " gesetzt auf:" + takePart);
        this.takePart = takePart;
    }
    
    
}
